package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dao.UserInfoMapper;
import mobi.hami.huapin.dao.UserWechatMapper;
import mobi.hami.huapin.model.UserInfo;
import mobi.hami.huapin.model.UserWechat;
import mobi.hami.huapin.service.UserWechatService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * Create by 子龙 on 2018/8/28.
 */
@Service
public class UserWechatServiceImpl implements UserWechatService {

    private static final Logger logger = LoggerFactory.getLogger(UserWechatServiceImpl.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private UserWechatMapper userWechatMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int save(UserWechat userWechat){
        return userWechatMapper.insertSelective(userWechat);
    }

    @Override
    public int update(UserWechat userWechat){
        return userWechatMapper.updateByPrimaryKeySelective(userWechat);
    }

    @Override
    public UserWechat selectOnebyId(Integer id){
        return userWechatMapper.selectByPrimaryKey(id);
    }

    @Override
    public UserWechat selectOneByWechatSmallId(String wechat_small_id){
        return userWechatMapper.selectByWechatSmallId(wechat_small_id);
    }

    @Override
    public void completUserInfo(Map<String, Object> map){
        logger.info("openid:"+map.get("openid").toString());
        UserWechat uwh  = userWechatMapper.selectByWechatSmallId(map.get("openid").toString());
        if(null != uwh){
            if(null != map.get("nickName") && !map.get("nickName").toString().trim().equals("")){
                uwh.setNick_name(map.get("nickName").toString());
            }
            if(null != map.get("userName") && !map.get("userName").toString().trim().equals("")){
                uwh.setUser_name(map.get("userName").toString());
            }
            if(null != map.get("avatarUrl") && !map.get("avatarUrl").toString().trim().equals("")){
                uwh.setHead_photo(map.get("avatarUrl").toString());
            }
            if(null != map.get("city") && !map.get("city").toString().trim().equals("")){
                uwh.setCity_info(map.get("city").toString());
            }
            if(null != map.get("unionId") && !map.get("unionId").toString().trim().equals("")){
                uwh.setWechat_union_id(map.get("unionId").toString());
            }
            userWechatMapper.updateByPrimaryKey(uwh);

            Integer userId = uwh.getUser_id();
            UserInfo info = userInfoMapper.selectById(userId);

            if(null != info){
                if(null != map.get("userName") && !map.get("userName").toString().trim().equals("")){
                    info.setUser_name(map.get("userName").toString());
                }
                if(null != map.get("nickName") && !map.get("nickName").toString().trim().equals("")){
                    info.setNick_name(map.get("nickName").toString());
                }
                if(null != map.get("phone") && !map.get("phone").toString().trim().equals("")){
                    info.setPhone(map.get("phone").toString());
                }
                if(null != map.get("avatarUrl") && !map.get("avatarUrl").toString().trim().equals("")){
                    info.setHead_photo(map.get("avatarUrl").toString());
                }else{
                    info.setHead_photo("http://mini.hami.mobi/images/huapin/HuapinHeadImg.png");
                }
                if(null != map.get("gender") && !map.get("gender").toString().trim().equals("")){
                    info.setGender(new Integer(map.get("gender").toString()));
                }
                userInfoMapper.updateByPrimaryKey(info);
            }
        }
    }


    /**
     * 根据用户Id　更新拼团参与次数
     * @param id
     * @return
     */
    @Override
    public int updatePartackCollageByUserId(Integer id) {
        return userWechatMapper.updatePartackCollageByUserId(id);
    }

    /**
     * 根据用户Id　更新发起拼团次数
     * @param id
     * @return
     */
    @Override
    public int updateLaunchCollageByUserId(Integer id) {
        return userWechatMapper.updateLaunchCollageByUserId(id);
    }

    /**
     * 根据用户Id 更新个人拼团次数
     * @param Id
     * @return
     */
    public int updateselfCollageByUserId(Integer Id){
        return userWechatMapper.updateselfCollageByUserId(Id);
    }

    @Override
    public UserWechat findModelbyUserId(Integer id) {
        return userWechatMapper.findModelbyUserId(id);
    }

    @Override
    public List<UserWechat> findModelByUserIdArray(List<Integer> Ids) {
        Assert.notNull(Ids, "Ids is not null");
        String arrValue= StringUtils.strip(Ids.toString(),"[]").trim();
        return userWechatMapper.findListbyUserIds(arrValue);
    }
}
